﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Device.Location;
using Microsoft.Phone.Controls.Maps;
using System.ComponentModel;

namespace Sisocana.PhoneApp.Views
{
    public partial class MapPage : PhoneApplicationPage, INotifyPropertyChanged
    {

        private const double DefaultZoomLevel = 17.0;
        private const double MaxZoomLevel = 21.0;
        private const double MinZoomLevel = 1.0;

        private readonly CredentialsProvider _credentialsProvider = new ApplicationIdCredentialsProvider(Assets.Resources.ApplicationStrings.BingMapsKey);
        private GeoCoordinate _center;
        private double _zoom;
        private Uri Icon { get; set; }

        public MapPage()
        {
            InitializeComponent();
            Map.ZoomBarVisibility = Visibility.Collapsed;
            Center = HelpRequestForm.Location;
            Zoom = DefaultZoomLevel;
            DataContext = this;
            Icon = new Uri("/Assets/Images/PushpinLocation.png", UriKind.Relative);
        }      

        private void ButtonZoomIn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Zoom += 1;
        }

        private void ButtonZoomOut_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Zoom -= 1;
        }

        private void ButtonSetPushpin_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Location = Center;
        }

        private void NotifyPropertyChanged(string propertyName)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public CredentialsProvider CredentialsProvider
        {
            get { return _credentialsProvider; }
        }

        public double Zoom
        {
            get { return _zoom; }
            set
            {
                var coercedZoom = Math.Max(MinZoomLevel, Math.Min(MaxZoomLevel, value));
                if (_zoom != coercedZoom) {
                    _zoom = value;
                    NotifyPropertyChanged("Zoom");
                }
            }
        }

        public GeoCoordinate Center
        {
            get { return _center; }
            set
            {
                if (_center != value) {
                    _center = value;
                    NotifyPropertyChanged("Center");
                }
            }
        }

        public GeoCoordinate Location
        {
            get { return HelpRequestForm.Location; }
            set
            {
                if (HelpRequestForm.Location != value) {
                    HelpRequestForm.Location = value;
                    NotifyPropertyChanged("Location");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        private void Pushpin_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
            Point pushpinLocation = Map.LocationToViewportPoint(Location);
            var x = pushpinLocation.X + e.DeltaManipulation.Translation.X;
            var y = pushpinLocation.Y + e.DeltaManipulation.Translation.Y;
            Location = Map.ViewportPointToLocation(new Point(x,y));
            e.Handled = true;
        }
    }
}